UVA 489 - Hangman Judge
原题链接:Hangman Judge
原题链接:Hangman Judge----vj

ATTENTION :我与书上的方法不一样。并且因为我很菜,用的方法会很笨,有什么想指点的请在评论区留言,勿喷👌。
题目描述
In “Hangman Judge,” you are to write a program that judges a series of Hangman games. For each
game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game
of hangman, and are given as follows:
1.The contestant tries to solve to puzzle by guessing one letter at a time.
2.Every time a guess is correct, all the characters in the word that match the guess will be “turnedover.” For example, if your guess is ‘o’ and the word is “book”, then both ‘o’s in the solution willbe counted as“solved”.
3.Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, whichneeds 7 strokes to complete. Each unique wrong guess only counts against the contestant once.

______
|  |
|  O
| /|\
|  |
| / \
 __|_
|  |______
|_________|

4.If the drawing of the hangman is completed before the contestant has successfully guessed all thecharacters of the word, the contestant loses.
5.If the contestant has guessed all the characters of the word before the drawing is complete, thecontestant wins the game.
6.If the contestant does not guess enough letters to either win or lose, the contestant chickens out.Your task as the “Hangman Judge” is to determine, for each game, whether the contestant wins,loses, or fails to finish a game.
Input
Your program will be given a series of inputs regarding the status of a game. All input will be in lowercase. The first line of each section will contain a number to indicate which round of the game is beingplayed; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of ‘-1’ would indicate the end of all games (and input).
Output
The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results:
You win.
You lose.
You chickened out.

Sample Input
1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1
Sample Output
Round 1
You win.
Round 2
You chickened out.
Round 3
You lose.

题意分析:计算机想一个单词让你猜,你每次可以猜一个字母,如果单词里有那个字母,所有该字母会显示出来;如果没有那个字母,则计算机会在一副“侩子手”画上填上一笔。这幅画一共需要7笔就能完成,因此你最多只能错6次。注意,猜一个已经猜过的字母也算错。(以上内容来自——算法竞赛入门经典第二版 刘汝佳编)。
题目中输入的第一个字符串就是计算机想的单词,第二个字符串就是你猜的所有字符,这道题书上和我在网上看的好多用的是用一个数组标记每个字母出现的次数,但是我不会用,于是用了map来实现。这题我WA了好几次,其实大致上的实现方式是差不多的,就是会有很多细节需要考虑。
题目中的输出有三种情况
You win. 即在错误7次一下成功猜出了所有第一个字符串中的字符。
You chickened out. 即虽然错误次数没有超过7,但是并没有猜出来所有的第一个字符串中的字符。
You lose. 即全部猜对之前错误次数大于等于7了。
我的解题思路:首先将第一个字符串中的所有元素用map标记value为1,然后开始询问第二个字符串中的元素,如果这个元素(即字符)的map值为一,就说明它在第一个字符串中存在,反之就说明这是一次错误的猜测,此时需要对错误的次数加一方便以后的判断。需要注意的是因为在判断的过程中如果错误的次数已经到达了7次,后面的就不需要再进行判断了,因为此时已经Lose了。还有就是每一个错误的字符最多错一次,解释一下就是如果s不是第一个字符串中的元素,尽管你在第二个字符串中出现了几个s,都还只是猜错了一次。还有一些具体的细节我们在代码里进行解释。
代码实现

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string x,y;
    while(cin>>n&&n!=-1)//题目要求n为-1时退出程序
    {
        map<char,int> ma;//用来标记第一个字符串中出现的字符
        map<char,int> ma1;//用来判断是否需要对错误次数进行累加
        getchar();
        cin>>x>>y;
        int p=0;
        for(int i=0;i<x.size();i++)
        {
                ma[x[i]]=1;//把第一个字符串中出现的字符全部存进map并标记为1
        }
        for(int i=0;i<y.size();i++)
        {
            if(ma[y[i]])//如果ma[y[i]]不为0,即说明这个元素在第一个字符串中
            {
                ma[y[i]]=0;//对最后的判断第一个字符串有没有被猜完做准备
                ma1[y[i]]=1;//因为这是一个正确的字符,所以标记为1,在接下来的判断中将其跳过
            }
            else if(!ma1[y[i]])//如果ma1[y[i]]为0,说明这个字符是错误且只出现了一次的,此时需要对错误次数加一
            {
                p++;
                ma1[y[i]]=1;//将y[i]标记为1,再遇见y[i]的时候,错误次数不变
            }
            if(p>=7)
                break;//错误的次数已经到达了7次,直接跳出循环
        }
        int flag=1;
        map<char,int>::iterator it;//定义一个迭代器,用来遍历map中的每一个元素
        for(it=ma.begin();it!=ma.end();it++)
        {
            if(it->second)//如果存在一个it->second不为0,即说明没有猜完
            {
                flag=0;
                break;
            }
        }
        printf("Round %d\n",n);//这个很坑,一开始就没注意到,注意看题目的输出格式!!!
        if(flag)//flag为1说明在7次错误以内已经猜对了所有字符,所以输出You win.            puts("You win.");
        else//否则对错误次数进行判定,这个里面挺好懂的,就不做详细解释了
        {
            if(p<7)
                puts("You chickened out.");
            else
                puts("You lose.");
        }
    }
    return 0;
}

至此这道题就告一段落了,再次说明一下,这种方法肯定是比较麻烦的那种,我知道😭
但是这是我能想起来的可以解决的方法了,以后好好了解一下数组在这方面的应用。
有什么想吐槽的就来吧,我听着

书上的方法

#include <stdio.h>
#include <string.h>
#define maxn 100
int left,chance;
char s[maxn],s2[maxn];
int win,lose;
void guess(char ch)
{
    int bad=1;
    for(int i=0;i<strlen(s);i++)
    if(s[i]==ch)
    {
        left--;
        s[i]=' ';
        bad=0;
    }
    if(bad)   --chance;
    if(!chance)  lose=1;
    if(!left)  win=1;
}
int main()
{
    int rnd;
    while(scanf("%d%s%s",&rnd,s,s2)==3&&rnd!=-1)
    {
        printf("Round %d\n",rnd);
        win=lose=0;
        left=strlen(s);
        chance=7;
        for(int i=0;i<strlen(s2);i++)
        {
            guess(s2[i]);
            if(win||lose)
            break;
        }
        if(win)
        printf("You win.\n");
        else if(lose)
        printf("You lose.\n");
        else
        printf("You chickened out.\n");
    }
    return 0;
}

书上的方法还是很直接的,直接看就可以看懂了😘